//
//  BaseNavController.swift
//  sinaDemo
//
//  Created by mac on 15/12/22.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit

class BaseNavController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置返回手势代理
        self.interactivePopGestureRecognizer?.delegate = self
    }

    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        // 子控制器不为0的时候添加返回item,为了实现自定义返回item,并且又要实现手势返回
        if childViewControllers.count != 0 {
            let button = UIButton(image: "navigationbar_back_withtext", backImage: nil, title: "返回", color: UIColor.lightGrayColor(), fontSize: 14)
            button.addTarget(self, action: "back", forControlEvents: UIControlEvents.TouchUpInside)
            let item = UIBarButtonItem(customView: button)
            viewController.navigationItem.leftBarButtonItem = item
        }
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func back() {
        popViewControllerAnimated(true)
    }
    
    // 是否允许手势开始
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count != 1
    }
}
